package com.gzx.spring.apis.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.DigestUtils;

import com.alibaba.fastjson.JSONObject;

public class XiaoniuUtil {
	private String appId;
	private String appSecret;
	private String apiUrl;

	public XiaoniuUtil appId(String appId) {
		this.appId = appId;
		return this;
	}

	public XiaoniuUtil appSecret(String appSecret) {
		this.appSecret = appSecret;
		return this;
	}

	public XiaoniuUtil apiUrl(String apiUrl) {
		this.apiUrl = apiUrl;
		return this;
	}

	private String sign(Map<String, String> paramValues, String secret) throws Exception {
		StringBuilder sb = new StringBuilder();
		List<String> paramNames = new ArrayList<>(paramValues.size());
		paramNames.addAll(paramValues.keySet());
		Collections.sort(paramNames);
		for (String paramName : paramNames) {
			sb.append(paramName).append(paramValues.get(paramName));
		}
		sb.append(secret);
		return genMd5(sb.toString());
	}

	private String genMd5(String data) {
		return DigestUtils.md5DigestAsHex(data.getBytes(StandardCharsets.UTF_8));
	}

	private String postHtml(String url, String postData) throws Exception {
		URL obj = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.print(postData);
		out.flush();
		BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
		StringBuffer response = new StringBuffer();
		String line;
		while ((line = br.readLine()) != null) {
			response.append(line);
		}
		br.close();
		return response.toString();
	}

	// R301
	public String r301(String name, String id, String phone) throws Exception {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("name", name);
		reqMap.put("mobile", phone);
		reqMap.put("idCard", id);

		String sign = sign(reqMap, appSecret);
		JSONObject bodyParams = new JSONObject();
		bodyParams.put("mobile", reqMap.get("mobile"));
		bodyParams.put("name", reqMap.get("name"));
		bodyParams.put("idCard", reqMap.get("idCard"));
		JSONObject params = new JSONObject();
		params.put("appId", appId);
		params.put("encType ", 1);
		params.put("sign", sign);
		params.put("body", bodyParams.toJSONString());

		return postHtml(apiUrl + "r301", params.toJSONString());
	}

	// R201
	public String r201(String name, String id, String phone) throws Exception {
		Map<String, String> reqMap = new HashMap<String, String>();
		reqMap.put("name", name);
		reqMap.put("mobile", phone);
		reqMap.put("idCard", id);

		String sign = sign(reqMap, appSecret);
		JSONObject bodyParams = new JSONObject();
		bodyParams.put("mobile", reqMap.get("mobile"));
		bodyParams.put("name", reqMap.get("name"));
		bodyParams.put("idCard", reqMap.get("idCard"));
		JSONObject params = new JSONObject();
		params.put("appId", appId);
		params.put("encType ", 1);
		params.put("sign", sign);
		params.put("body", bodyParams.toJSONString());

		return postHtml(apiUrl + "riskScore6", params.toJSONString());
	}
}
